<template>
  <div class="selectCompany">
    <inputSelect :isShowCity="false" :contentData.sync="companyList" :JFData.sync="JFData" :IStitle="false" :title="''" @getValues="getValues" />
    <div style="background-color: #f6f6f6; height: 14px; margin-top: 4px"></div>
    <h2 style="padding: 10px">{{ JFData[0].IF_XT == '1' ? '工匠信息' : '企业信息' }}</h2>
    <inputSelect :isShowCity="false" :contentData.sync="companyList1" :JFData.sync="JFData" :title="''" @getValues="getValues" @getButton1="getButton" />
    <div style="display: flex; justify-content: center; align-items: center; margin-top: 20%">
      <van-button type="primary" @click="handleToClick" style="width: 90vw">提交</van-button>
      <!-- <van-button v-else type="primary" @click="handleToClick_next" style="width: 90vw">下一步</van-button> -->
    </div>
  </div>
</template>

<script>
import inputSelect from '../NewZzd/public/inputSelect'
import { addDate, GSA_Search, GSA_Add } from '@/common/commonApi'
import { v4 as uuidv4 } from 'uuid'
import { houseCraftsmanprojectSave } from '@/utils/api'
import { GJDBFunction2 } from '@/utils/queryData'
export default {
  data() {
    return {
      isnew: false,
      companyList: [
        {
          label: '选工匠/选企业',
          key: 'if_xt',
          ISinput: false,
          required: true,
          readonly: true,
          isTime: false,
          //   buttonTest: '选择',
          emitData: '',
          NYDARR: [
            {
              DICNAME: '工匠',
              DICID: '1'
            },
            {
              DICNAME: '企业',
              DICID: '2'
            }
          ]
        }
      ],
      companyList1: [
        {
          label: '工匠姓名',
          key: 'artisanName', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: false, //true:可以填写 false：不可以填写
          isTime: false, //true:可以时间选择器 false：下拉框
          buttonTest: '选工匠'
        },
        {
          label: '工匠手机号',
          key: 'phone', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: false, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        },
        {
          label: '证书编号',
          key: 'bookNumberNew', //绑定的字段
          ISinput: true, //true:输入框
          required: false, //true: 必填项
          readonly: false, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        },
        {
          label: '工匠星级',
          key: 'artisanGrade', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: false, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        }
      ],
      JFData: [
        {
          IF_XT: '',
          artisanGrade: '',
          artisanName: '',
          bookNumberNew: '',
          idCard: '',
          phone: '',
          qymc: '',
          zzjgdm: '',
          lxr: '',
          dwdz: '',
          aqscxkbh: '',
          frdh: ''
        }
      ],
      GJData: {},
      //用于对照
      DuiZhaoData: {
        //工匠姓名
        CRAFTSMANNAME: 'artisanName',
        CRAFTSMANID_CARD: 'idCard',
        //工匠手机号
        CRAFTSMANPHONE: 'phone',
        //证书编号
        ZSBH: 'bookNumberNew',
        //工匠星级
        GJXJ: 'artisanGrade',
        //企业名称
        ENTERPRISENAME: 'qymc',
        //企业地址
        ENTERPRISEDDDRESS: 'dwdz',
        //企业法人
        LEGALPERSON: 'lxr',
        //企业电话
        CONTACTNUMBER: 'frdh',
        // 资格证书编号
        CONSTRUCTIONNUMBER: 'aqscxkbh',
        //统一税号
        ENTERPRISECODE: 'zzjgdm'
      }
    }
  },
  mounted() {
    // this.isnew = this.$store.state.VERSIONNUMBER == 1&&window.localStorage.getItem('newData') == 1 ? true : false
    let data = window.sessionStorage.getItem('applyFor')?JSON.parse(window.sessionStorage.getItem('applyFor')):null
    if (data) {
      for (let i in this.DuiZhaoData) {
        for (let j in this.JFData[0]) {
          if (this.DuiZhaoData[i] == j) {
            this.JFData[0][j] = data[i]
          }
        }
      }
      if (data.TYPE == '59' || data.JZCS <3) {
        this.getValues('工匠')
        this.JFData[0].IF_XT = '1'
      } else if(data.JZCS>=3){
        this.JFData[0].IF_XT = '2'
        this.getValues('企业')
      } else if(data.TYPE == '60'){
        this.JFData[0].IF_XT = '2'
        this.getValues('企业')
      }
    }
    try {
      this.GJData = JSON.parse(this.$route.query?.data ? this.$route.query?.data : this.$route.query?.COMAPNY_NAME)
    } catch (e) {}
    console.log(this.GJData, 'this.GJData')
    for (let i in this.GJData) {
      for (let j in this.JFData[0]) {
        if (j == i) {
          this.JFData[0][j] = this.GJData[i]
        }
      }
    }
  },
  components: {
    inputSelect
  },
  methods: {
    handleToClick_next() {},
    getValues(name, value, label) {
      if (name == '企业') {
        this.companyList1 = [
          {
            label: '企业名称',
            key: 'qymc', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false, //true:可以时间选择器 false：下拉框
            buttonTest: '选企业'
          },
          {
            label: '统一税号',
            key: 'zzjgdm', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '法人',
            key: 'lxr', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '联系电话',
            key: 'frdh', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '资质证书编号',
            key: 'aqscxkbh', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '公司地址',
            key: 'dwdz', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          }
        ]
      } else {
        this.companyList1 = [
          {
            label: '工匠姓名',
            key: 'artisanName', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false, //true:可以时间选择器 false：下拉框
            buttonTest: '选工匠'
          },
          {
            label: '工匠手机号',
            key: 'phone', //绑定的字段
            ISinput: true, //true:输入框
            required: true, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '证书编号',
            key: 'bookNumberNew', //绑定的字段
            ISinput: true, //true:输入框
            required: false, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          },
          {
            label: '工匠星级',
            key: 'artisanGrade', //绑定的字段
            ISinput: true, //true:输入框
            required: false, //true: 必填项
            readonly: false, //true:可以填写 false：不可以填写
            isTime: false //true:可以时间选择器 false：下拉框
          }
        ]
      }
      // if (this.isnew) {
      //   this.companyList1[0].buttonTest = ''
      // }
    },
    async handleToClick() {
      //新增逻辑 this.$route.query.isadd 为true
      // window.sessionStorage.setItem('applyFor', '')
      let newObj = {}
      for (let i in this.JFData[0]) {
        for (let j in this.DuiZhaoData) {
          if (i == this.DuiZhaoData[j]) {
            newObj[j] = this.JFData[0][i]
          }
        }
      }
      let uuid1 = uuidv4()
      let row = window.sessionStorage.getItem('applyFor') ? JSON.parse(window.sessionStorage.getItem('applyFor')) : {}
      let isadd = window.sessionStorage.getItem('isadd')
      newObj.ROW_GUID = isadd == 2 ? uuid1 : row.ROW_GUID
      newObj.TO_ROW_GUID = row.TO_ROW_GUID
      isadd == 2 ? '' : (newObj.SGDWPKID = row.SGDWPKID)
      if (this.JFData[0].IF_XT == '1') {
        newObj.TYPE = '59'
        newObj.ENTERPRISENAME = ''
        newObj.ENTERPRISEDDDRESS = ''
        newObj.LEGALPERSON = ''
        newObj.CONTACTNUMBER = ''
        newObj.CONSTRUCTIONNUMBER = ''
        newObj.ENTERPRISECODE = ''
      } else {
        newObj.TYPE = '60'
        newObj.CRAFTSMANNAME = ''
        newObj.CRAFTSMANPHONE = ''
        newObj.ZSBH = ''
        newObj.GJXJ = ''
      }
      //修改逻辑
      const date = await GSA_Add('7c3a7582-4a27-4fab-a5b3-b2da427cb989', isadd == 1 ? false : true, newObj)
      //修改成功之后要取消合同关联
      let strParams = { PKID: row.PKID, HTBHQDZT: '0', HTQDZT: '0', F_ISSAVEHT: '0', HTQDRQ: null, HTJE: null, HTJZMJ: null, HTFWCG: null, XXHT_XMFZR: null, XXHT_SGFZR: null }
      const results = await GSA_Add('89d035c0-5095-45f2-bece-fa7eecc5c732', false, strParams)
      let pass = { TO_ROW_GUID: row.TO_ROW_GUID, GROUPGUID: '2774cbb5-1854-4a0a-afaf-bee2e8f83cab' }
      const results1 = await GSA_Add('89d035c0-5095-45f2-bece-fa7eecc5c732', false, strParams)
      //选择工匠时调用
      if (this.JFData[0].IF_XT == '1') {
        const res22 = await GJDBFunction2('get_gjcjxmsj', [{ get_gjcjxmsj: JSON.parse(window.sessionStorage.getItem('ROW_GUID1')).ROW_GUID }])
        let params = {
          ID: res22[0].ID,
          commencementtime: res22[0].COMMENCEMENTTIME,
          completiontime: res22[0].COMPLETIONTIME,
          houseapplicant: res22[0].HOUSEAPPLICANT,
          construcitonaddress: res22[0].CONSTRUCITONADDRESS,
          Planningpermit: res22[0].PLANNINGPERMIT,
          buildstorey: res22[0].BUILDSTOREY,
          buildarea: res22[0].BUILDAREA,
          craftsmancode: res22[0].CRAFTSMANCODE,
          city: res22[0].CITY,
          county: res22[0].COUNTY,
          town: res22[0].TOWN,
          village: res22[0].VILLAGE,
          phone: res22[0].PHONE,
          housebuilding: res22[0].HOUSEBUILDING,
          rowGuid: res22[0].ROWGUID,
          applytime: res22[0].APPLYTIME,
          years: res22[0].YEARS,
          contractid: res22[0].CONTRACTID,
          isWhole: res22[0].ISWHOLE,
          isCompleted: res22[0].ISCOMPLETED,
          projectAmount: res22[0].PROJECTAMOUNT,
          isOnline: res22[0].ISONLINE
        }
        const res = await houseCraftsmanprojectSave(params)
      }
      window.sessionStorage.setItem('applyFor', '')
      this.$router.push({
        path: '/zljindex',
        query: {
          data: window.sessionStorage.getItem('ROW_GUID1')
        }
      })
    },
    getButton(data) {
      console.log(data)
      if (data.buttonTest == '选工匠') {
        this.$router.push({
          path: '/craftsmanServices',
          query: {
            id: this.isnew ? '' : 'NewChooseCraftsman'
          }
        })
      } else {
        this.$router.push({
          path: '/appraisalCompany',
          query: {
            id: 'appraisalCompanyNew'
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.selectCompany {
  width: 100%;
  height: 100vh;
  overflow: auto;
  padding: 6px;
}
</style>
